package com.qs.education.controller;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.qs.education.bean.api.PageBean;
import com.qs.education.bean.api.ResponseListBean;
import com.qs.education.bean.beans.EducationClassBean;
import com.qs.education.bean.beans.EducationGradeBean;
import com.qs.education.bean.beans.EducationSchoolBean;
import com.qs.education.bean.beans.EducationStudentResponse;
import com.qs.education.bean.entity.EducationStudent;
import com.qs.education.service.IClassService;
import com.qs.education.service.IGradeService;
import com.qs.education.service.ISchoolService;
import com.qs.education.service.IStudentService;

/**
 * 
 * @class StudentController
 * @author Jason
 * @description
 * @date Nov 8, 2018 2:13:57 PM
 */
@Controller
public class StudentController {

	@Autowired
	private IStudentService iStudentService;
	@Autowired
	private ISchoolService iSchoolService;
	@Autowired
	private IGradeService iGradeService;
	@Autowired
	private IClassService iClassService;
	
	private final Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * @Description 查询所有学生
	 * @author Jason
	 * @date Nov 19, 2018
	 * @return
	 */
	@RequiresPermissions(value={"user:view"})
	@RequestMapping("/getStudent")
	public ModelAndView getStudent(EducationStudent student,
			@RequestParam(defaultValue="1",value="currentPage") Integer currentPage,
			@RequestParam(defaultValue="10",value="pageSize") Integer pageSize){
		
		log.info("后台管理-->查询所有学生");
		ModelAndView mv=new ModelAndView("user/student/list");
		PageBean pageBean =iStudentService.getEducationStudent(student,currentPage,pageSize);
		mv.addObject("studentPageBean", pageBean);
		mv.addObject("student", student);
		
		return mv;
	}
	
	/**
	 * @Description 根据ID删除学生
	 * @author Jason
	 * @date Nov 8, 2018
	 * @param studentId
	 * @return
	 */
	@RequiresPermissions(value={"user:delete"})
	@RequestMapping("/deleteStudent")
	public ModelAndView deleteStudentById(EducationStudent student,
			@RequestParam(defaultValue="1",value="currentPage") Integer currentPage,
			@RequestParam(defaultValue="10",value="pageSize") Integer pageSize,
			RedirectAttributes attr){
		
		log.info("后台管理-->删除学生");
		ModelAndView mv=new ModelAndView("forward:/getStudent");
		attr.addFlashAttribute("currentPage", currentPage);
		attr.addFlashAttribute("pageSize", pageSize);
		mv.addObject("student", student);
		EducationStudentResponse educationStudentResponse = iStudentService.deleteStudentById(student.getId().intValue());
		mv.addObject("educationStudentResponse", educationStudentResponse);
		
		return mv;
	}
	
	/**
	 * 
	 * @Description 根据ID查询学生
	 * @author Jason
	 * @date Nov 8, 2018
	 * @param studentId
	 * @return
	 */
	@RequiresPermissions(value={"user:view"})
	@RequestMapping("/findStudent")
	public ModelAndView findStudentById(Integer studentId){
		
		log.info("后台管理-->根据ID查询学生");
		ModelAndView mv=new ModelAndView("user/student/update");
		EducationStudentResponse educationStudentResponse = iStudentService.findStudentById(studentId);
		mv.addObject("educationStudentResponse", educationStudentResponse);
		return mv;
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder, WebRequest request) {
		// ，转换日期
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
	}
	/**
	 * 
	 * @Description 修改学生信息
	 * @author Jason
	 * @date Nov 8, 2018
	 * @param studentId
	 * @return
	 */
	@RequiresPermissions(value={"user:update"})
	@RequestMapping("/updateStudent")
	public ModelAndView updateStudent(EducationStudent educationStudent){
		
		log.info("后台管理-->修改学生信息");
		System.out.println(educationStudent);
		ModelAndView mv=new ModelAndView("forward:/getStudent");
		EducationStudentResponse educationStudentResponse = iStudentService.updateStudent(educationStudent);
		
		return mv;
	}
	
	/**
	 * @Description 添加学生
	 * @author Jason
	 * @date Nov 9, 2018
	 * @param educationStudent
	 * @return
	 */
	@RequiresPermissions(value={"user:add"})
	@RequestMapping("/addStudent")
	public ModelAndView addStudent(EducationStudent educationStudent){
		
		log.info("后台管理-->添加学生信息");
		System.out.println(educationStudent);
		ModelAndView mv=new ModelAndView("forward:/getStudent");
		EducationStudentResponse educationStudentResponse = iStudentService.addStudent(educationStudent);
		mv.addObject("educationStudentResponse", educationStudentResponse);
		
		return mv;
	}
	
	/**
	 * @Description 添加学生页面跳转
	 * @author Jason
	 * @date Nov 9, 2018
	 * @param educationStudent
	 * @return
	 */
	@RequestMapping("/addStudentHTML")
	public ModelAndView addStudentHTML(){
		
		log.info("后台管理-->添加学生信息跳转");
		ModelAndView mv=new ModelAndView("user/student/add");
		ResponseListBean<EducationSchoolBean> schoolList = iSchoolService.getSchoolList();
		mv.addObject("schools", schoolList);
		ResponseListBean<EducationGradeBean> gradeList = iGradeService.getGradeList();
		mv.addObject("grades", gradeList);
		ResponseListBean<EducationClassBean> classList = iClassService.getClassList();
		mv.addObject("clazzs", classList);
	
		return mv;
	}
	
	/**
	 * @Description 添加学生页面跳转
	 * @author Jason
	 * @date Nov 9, 2018
	 * @param educationStudent
	 * @return
	 */
	@RequestMapping("/updateStudentHTML")
	public ModelAndView updateStudentHTML(Integer studentId){
		
		log.info("后台管理-->更新学生信息跳转");
		ModelAndView mv=new ModelAndView("user/student/update");
		EducationStudentResponse educationStudentResponse = iStudentService.findStudentById(studentId);
		mv.addObject("educationStudentResponse", educationStudentResponse);
		ResponseListBean<EducationSchoolBean> schoolList = iSchoolService.getSchoolList();
		mv.addObject("schools", schoolList);
		ResponseListBean<EducationGradeBean> gradeList = iGradeService.getGradeList();
		mv.addObject("grades", gradeList);
		ResponseListBean<EducationClassBean> classList = iClassService.getClassList();
		mv.addObject("clazzs", classList);
	
		return mv;
	}
	
	/**
	 * @Description 根据条件搜索学生
	 * @author Jason
	 * @date Nov 12, 2018
	 * @param studentId
	 * @return
	 */
	@RequestMapping("/findStudentByCondition")
	public ModelAndView findStudentByCondition(EducationStudent student){
		
		log.info("后台管理-->根据条件搜索学生");
		ModelAndView mv=new ModelAndView("user/student/list");
		ResponseListBean<EducationStudent> educationStudentList = iStudentService.findStudentByCondition(student);
		mv.addObject("educationStudentList", educationStudentList);
	
		return mv;
	}
}
